package org.ws.edu.task2.commands;

import org.ws.edu.task2.Element;
import org.ws.edu.task2.ICommand;
import org.ws.edu.task2.VirtualFileSystem;
import org.ws.edu.task2.exceptions.ExtensionLengthException;
import org.ws.edu.task2.exceptions.IncorrectPathException;
import org.ws.edu.task2.exceptions.IncorrectSymbolsException;
import org.ws.edu.task2.exceptions.NameLengthException;

public class Remove implements ICommand {
    private String[] path = null;
    private Element element = null;

    public Remove(String s) {
        path = s.split("\\\\");
    }

    @Override
    public void execute(VirtualFileSystem vfs) throws IncorrectPathException, ExtensionLengthException, NameLengthException,
            IncorrectSymbolsException {
        validPath(vfs);
        if (vfs.isCurrentFolder(element)) {
            vfs.removeElement(new Element(path[path.length - 1], true));
        } else if (element.elements.contains(vfs.getCurrentFolder())) {
            vfs.changeCurrentElement(element);
            vfs.removeElement(new Element(path[path.length - 1], true));
        } else {
            throw new IncorrectPathException();
        }

    }

    @Override
    public void validPath(VirtualFileSystem vfs) throws ExtensionLengthException, NameLengthException, IncorrectSymbolsException,
            IncorrectPathException {
        element = vfs.getElementByPath(path);

    }
}
